package com.example.poemtest.index.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.example.poemtest.Activity.BottomNavigationViewActivity;
import com.example.poemtest.R;
import com.example.poemtest.entity.DynastryPoetryBean;
import com.example.poemtest.utils.Constant;
import com.example.poemtest.utils.FullScreenUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Random;

/**
 * 诗词鉴赏页面
 */
public class PoetryAdmireActivity extends AppCompatActivity {

    private TextView poetryTitle, poetryAuther, poetryDynasty, poetryContent, poetryDesc;
    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FullScreenUtil.getInstance().fullScreen(this,true);
        setContentView(R.layout.activity_poetry_admire);

        //绑定控件对象
        setView();
        //加载数据
        loadData();

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PoetryAdmireActivity.this, BottomNavigationViewActivity.class);
                intent.putExtra("id",1);
                startActivity(intent);
            }
        });
    }

    private void setView() {
        back = findViewById(R.id.setup_safe_to_mine);
        poetryTitle = findViewById(R.id.poetry_title);
        poetryAuther = findViewById(R.id.poetry_author);
        poetryDynasty = findViewById(R.id.poetry_dynasty);
        poetryContent = findViewById(R.id.poetry_content);
        poetryDesc = findViewById(R.id.poetry_desc);
    }

    /**
     * 加载数据
     */
    private void loadData() {
        //随机获取json文件中的数据
//        String json = getJson(this, "dynastypoetry.json");
//        DynastryPoetryBean dynastryPoetryBean = JSONObject.parseObject(json,DynastryPoetryBean.class);
//        List<DynastryPoetryBean.DynastyPoetry> list1 = dynastryPoetryBean.getData();
        //产生随机数
        Random rand = new Random();
        int num = rand.nextInt(20) + 1;
        poetryTitle.setText(Constant.poetryInfos.get(num).getPoetryTitle());
        poetryAuther.setText(Constant.poetryInfos.get(num).getPoetryAuthor());
        poetryDynasty.setText(Constant.poetryInfos.get(num).getPoetryDynasty());
        poetryContent.setText(Constant.poetryInfos.get(num).getPoetryContent());
        poetryDesc.setText(Constant.poetryInfos.get(num).getPoetryTranslation());
    }

    /**
     * 得到json文件中的内容
     *
     * @param context
     * @param fileName
     * @return
     */
    public static String getJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        //获得assets资源管理器
        AssetManager assetManager = context.getAssets();
        //使用IO流读取json文件内容
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName), "utf-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

}